﻿using System;

namespace ch05.binary
{
    public static class Bits
    {
        public static uint RotateLeft32(uint x, int k)
        {
            const uint n = 32u;
            var s = (uint) (k) & (n - 1);
            return x << (int) s | x >> (int) (n - s);
        }

        // RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
        // To rotate x right by k bits, call RotateLeft64(x, -k).
        //
        // This function's execution time does not depend on the inputs.
        public static ulong RotateLeft64(ulong x, int k)
        {
            const ulong n = 64;
            var s = (uint) (k) & (n - 1);
            return x << (int) s | x >> (int) (n - s);
        }

        // LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
        public static int LeadingZeros32(uint x)
        {
            return 32 - Len32(x);
        }

        // LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
        public static int LeadingZeros64(ulong x)
        {
            return 64 - Len64(x);
        }

        // Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
        private static int Len32(uint x)
        {
            var n = 0;
            if (x >= 1 << 16)
            {
                x >>= 16;
                n = 16;
            }

            if (x >= 1 << 8)
            {
                x >>= 8;
                n += 8;
            }

            return n + Len8Tab[x];
        }

        // Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
        private static int Len64(ulong x)
        {
            var n = 0;
            if (x >= 1ul << 32)
            {
                x >>= 32;
                n = 32;
            }

            if (x >= 1 << 16)
            {
                x >>= 16;
                n += 16;
            }

            if (x >= 1 << 8)
            {
                x >>= 8;
                n += 8;
            }

            return n + Len8Tab[x];
        }

        // See http://supertech.csail.mit.edu/papers/debruijn.pdf
        private const int DeBruijn32 = 0x077CB531;
        private const long DeBruijn64 = 0x03f79d71b4ca8b09;

        // TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
        public static int TrailingZeros32(uint x)
        {
            // see comment in TrailingZeros64
            return x == 0 ? 32 : DeBruijn32Tab[((int) (x & -x) * DeBruijn32) >> (32 - 5)];
        }

        // OnesCount32 returns the number of one bits ("population count") in x.
        public static int OnesCount32(uint x)
        {
            return Pop8Tab[x >> 24] + Pop8Tab[x >> 16 & 0xff] + Pop8Tab[x >> 8 & 0xff] + Pop8Tab[x & 0xff];
        }

        private static readonly byte[] Pop8Tab =
        {
            0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
            0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
            0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
            0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
            0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
            0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
            0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
            0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08,
        };

        private static readonly byte[] DeBruijn32Tab =
        {
            0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
            31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
        };

        // TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
        public static int TrailingZeros64(ulong x)
        {
            // If popcount is fast, replace code below with return popcount(^x & (x - 1)).
            //
            // x & -x leaves only the right-most bit set in the word. Let k be the
            // index of that bit. Since only a single bit is set, the value is two
            // to the power of k. Multiplying by a power of two is equivalent to
            // left shifting, in this case by k bits. The de Bruijn (64 bit) constant
            // is such that all six bit, consecutive substrings are distinct.
            // Therefore, if we have a left shifted version of this constant we can
            // find by how many bits it was shifted by looking at which six bit
            // substring ended up at the top of the word.
            // (Knuth, volume 4, section 7.3.1)
            return x == 0 ? 64 : DeBruijn64Tab[(ulong) (((long) x & -(long) x) * DeBruijn64) >> (64 - 6)];
        }

        // OnesCount64 returns the number of one bits ("population count") in x.
        public static int OnesCount64(ulong x)
        {
            // Implementation: Parallel summing of adjacent bits.
            // See "Hacker's Delight", Chap. 5: Counting Bits.
            // The following pattern shows the general approach:
            //
            //   x = x>>1&(m0&m) + x&(m0&m)
            //   x = x>>2&(m1&m) + x&(m1&m)
            //   x = x>>4&(m2&m) + x&(m2&m)
            //   x = x>>8&(m3&m) + x&(m3&m)
            //   x = x>>16&(m4&m) + x&(m4&m)
            //   x = x>>32&(m5&m) + x&(m5&m)
            //   return int(x)
            //
            // Masking (& operations) can be left away when there's no
            // danger that a field's sum will carry over into the next
            // field: Since the result cannot be > 64, 8 bits is enough
            // and we can ignore the masks for the shifts by 8 and up.
            // Per "Hacker's Delight", the first line can be simplified
            // more, but it saves at best one instruction, so we leave
            // it alone for clarity.
            const ulong m = ulong.MaxValue;
            x = (x >> 1 & (M0 & m)) + (x & (M0 & m));
            x = (x >> 2 & (M1 & m)) + (x & (M1 & m));
            x = ((x >> 4)+x) & (M2 & m);
            x += x >> 8;
            x += x >> 16;
            x += x >> 32;
            return (int) (x) & ((1 << 7) - 1);
        }


        // --- OnesCount ---
        private const ulong M0 = 0x5555555555555555; // 01010101 ...
        private const ulong M1 = 0x3333333333333333; // 00110011 ...
        private const ulong M2 = 0x0f0f0f0f0f0f0f0f; // 00001111 ...
        private const ulong M3 = 0x00ff00ff00ff00ff; // etc.
        private const ulong M4 = 0x0000ffff0000ffff;

        private static readonly byte[] DeBruijn64Tab =
        {
            0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
            62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
            63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
            54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
        };

        private static readonly byte[] Len8Tab =
        {
            0x00, 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
            0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
            0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
            0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
            0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
            0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
            0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
            0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
        };
    }
}